package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.AppendixAddDTO;
import org.jsola.contract.dto.AppendixUpdateDTO;
import org.jsola.contract.query.AppendixQuery;
import org.jsola.contract.service.IAppendixService;
import org.jsola.contract.vo.AppendixListSiteIdVO;
import org.jsola.contract.vo.AppendixListVO;
import org.jsola.contract.vo.AppendixVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 合同文件
 *
 * @author JanusMix
 */
@RestController("contractAppendixController")
@RequestMapping(ContractConstants.API_PATH + "/v1/appendixes")
public class AppendixController implements IAppendixControllerAPI {

    @Autowired
    private IAppendixService appendixService;

    @Override
    @PostMapping
    public Result<AppendixVO> save(@Valid @RequestBody AppendixAddDTO appendixAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增合同文件权限校验

        return Result.success(
                appendixService.save(appendixAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AppendixUpdateDTO appendixUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改合同文件权限校验

        int count = appendixService.update(appendixUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{appendixId}")
    public Result<Integer> delete(@PathVariable Long appendixId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除合同文件权限校验

        int count = appendixService.deleteByIds(tokenUser, appendixId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] appendixIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除合同文件权限校验

        int count = appendixService.deleteByIds(tokenUser, appendixIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{appendixId}")
    public Result<AppendixVO> findById(@PathVariable Long appendixId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同文件权限校验

        return Result.success(
                appendixService.selectById(appendixId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AppendixListVO>> listAll(@Valid AppendixQuery appendixQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同文件权限校验

        return Result.success(
                appendixService.select(appendixQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/selectByContractId/{contractIdStr}")
    public Result<AppendixVO> selectByContractId(@PathVariable String contractIdStr) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同文件权限校验

        ParamException.isFalse("undefined".equals(contractIdStr), "合同正在生成中，请稍后再试");
        long contractId;
        try {
            contractId = Long.parseLong(contractIdStr);
        } catch (NumberFormatException e) {
            throw new ParamException("合同生成有误，请刷新后重试");
        }
        return Result.success(
                appendixService.selectVOByContractId(contractId, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AppendixListVO>> page(@Valid AppendixQuery appendixQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                appendixService.selectPage(appendixQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PutMapping(value = "/renameContractPdf")
    public Result<String> renameContractPdf() {
        TokenUser tokenUser = UserContext.getUser();
        appendixService.renameContractPdf(tokenUser);
        return Result.success("修改成功");
    }

    @Override
    @GetMapping(value = "/listAllNoSiteId")
    public Result<List<AppendixListSiteIdVO>> listAllNoSiteId(@Valid AppendixQuery appendixQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同文件权限校验

        return Result.success(
                appendixService.listAllNoSiteId(appendixQuery)
        );
    }


}

